<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

class vm_ps_restaurant {
    function edit($vendor_id) {
        $db = new ps_DB;
        $vendor_id = (int)$vendor_id;
        
        $q = "SELECT *, u.username as vendor_user_name, u.password as vendor_password,
                u.email as vendor_email, c.category_parent_id, c.category_child_id
              FROM #__{vm}_vendor v
                LEFT JOIN #__{vm}_vendor_locations v_l ON v.vendor_id = v_l.vendor_id
                LEFT JOIN #__users u ON v.vendor_user_id = u.id
                LEFT JOIN (SELECT c.vendor_id, c_x.category_parent_id, c_x.category_child_id
                           FROM #__{vm}_category c
                                INNER JOIN #__{vm}_category_xref c_x ON c.category_id = c_x.category_child_id
                           WHERE c_x.category_parent_id != 0) c
                  ON v.vendor_id = c.vendor_id
                LEFT JOIN #__{vm}_vendor_shipping v_s ON v_s.vendor_id = v.vendor_id  
              WHERE v.vendor_id = ".$vendor_id;
        
        $db->query($q);  
        $db->next_record();
        
        $db->phone_1 = explode('-', $db->f('contact_phone_1'));
        $db->phone_2 = explode('-', $db->f('contact_phone_2'));
        
        $db->kitchens = vm_ps_restaurant::getKitchensHours($vendor_id);
        $db->daysClosed = vm_ps_restaurant::getDaysClosed($vendor_id);
        $db->cuisine = vm_ps_restaurant::getCuisine($vendor_id);
        
        return $db;
    }
    
    function getKitchensHours($vendor_id) {
        $db = new ps_DB;
        
        $q = "SELECT * 
              FROM #__{vm}_vendor_kitchens_hours
              WHERE vendor_id='$vendor_id'";
              
        $db->query($q);  
        $db->next_record();
        
        return $db;
    }    
    
    function getDaysClosed($vendor_id) {
        $db = new ps_DB;
        
        $q = "SELECT *
              FROM #__{vm}_vendor_days_closed
              WHERE vendor_id='$vendor_id'
              ORDER BY day_closed";
              
        $db->query($q);  
        $db->next_record();
        
        return $db;
    }
    
    function getCuisine($vendor_id) {
        $db = new ps_DB;
        
        $q = "SELECT * 
              FROM #__{vm}_vendor_cuisine
              WHERE vendor_id='$vendor_id'";
              
        $db->query($q);  
        $db->next_record();
        
        return $db;
    }
    
    function getCuisineType($value = 0) {
        $db = new ps_DB;

        $q = "SELECT id, name FROM #__{vm}_cuisine_type ORDER BY name";
        $db->query($q);
        
        $sScript = '<option value="0">-- Select a cuisine --</option>';
        while ($db->next_record()) {
            if ((int)$db->f("id") == $value) {
                $sScript .= '<option value="'.$db->f("id").'" selected="selected">'.$db->f("name").'</option>';
            } else {
                $sScript .= '<option value="'.$db->f("id").'">'.$db->f("name").'</option>';
            }
        }
                
        echo $sScript;
    }
    
    function getDayHtml($val = "") {
        $arrName = array("-1" => "Select day",
                         "1" => "Monday",
                         "2" => "Tuesday",
                         "3" => "Wednesday",
                         "4" => "Thusday",
                         "5" => "Friday",
                         "6" => "Saturday",
                         "7" => "Sunday",);
                    
        $sScript = '';     
        foreach($arrName as $key=>$value) {
            if ($val == $key) {
                $sScript .= '<option value="'.$key.'" selected="selected">'.$value.'</option>';
            } else {
                $sScript .= '<option value="'.$key.'">'.$value.'</option>';
            }
        }
        
        return $sScript;
    }
    
    function getHourHtml($value = -1) {
        $sScript = '';
        
        $val = '0';
        $isSelected = false;
        if ($value > 12) { // PM
            $value = $value - 12;
        }
            
        for($i=1;$i<13;$i++) {
            if ($i<10) {
                $val = '0'.$i;
            } else {
                $val = $i;
            }
            
            if ($value == $i) {
                $isSelected = true;
                $sScript .= '<option value="'.$i.'" selected="selected">'.$val.'</option>';
            } else {
                $sScript .= '<option value="'.$i.'">'.$val.'</option>';
            }            
        }
        
        if ($isSelected) {
            $sScript = '<option value="-1">HH</option>'.$sScript;
        } else {
            $sScript = '<option value="-1" selected="selected">HH</option>'.$sScript;
        }
        
        echo $sScript;
    }
    
    function getAmPmHtml($value = -1) {
        $sScript = '';
                
        if ($value == -1) { // PM
            $sScript = '<option value="-1" selected="selected">--</option>';
            $sScript .= '<option value="1">AM</option>';
            $sScript .= '<option value="2">PM</option>';
        } else if ($value > 12) { // PM
            $sScript = '<option value="-1">--</option>';
            $sScript .= '<option value="1">AM</option>';
            $sScript .= '<option value="2" selected="selected">PM</option>'; 
        } else { // AM
            $sScript = '<option value="-1">--</option>';
            $sScript .= '<option value="1" selected="selected">AM</option>';
            $sScript .= '<option value="2">PM</option>';
        }
                
        echo $sScript;
    }
    
    function getMinuteHtml($value = -1) {
        $sScript = '';
        
        $val = '0';
        $isSelected = false;
        for($i=0;$i<60;$i++) {
            if ($i<10) {
                $val = '0'.$i;
            } else {
                $val = $i;
            }
            
            if ($value == $i) {
                $isSelected = true; 
                $sScript .= '<option value="'.$i.'" selected="selected">'.$val.'</option>';
            } else {
                $sScript .= '<option value="'.$i.'">'.$val.'</option>';
            }
        }
        
        if ($isSelected) {
            $sScript = '<option value="-1">MM</option>'.$sScript;
        } else {
            $sScript = '<option value="-1" selected="selected">MM</option>'.$sScript;
        }
        
        echo $sScript;
    }
        
    function add( &$d ) {
        // Insert to the users table
        $user_id = vm_ps_restaurant::addUsers($d);

        // Insert to the user_info table
        vm_ps_restaurant::addUsersVmInfo($d, $user_id);
        
        // Insert to the core_acl_aro table
        $aro_id = vm_ps_restaurant::addCoreAclAro($d, $user_id);
        
        // Insert to the core_acl_group_aro_map table
        vm_ps_restaurant::addCoreAclGroupAroMap($aro_id);
                
        // Insert to the vendor table
        $vendor_id = vm_ps_restaurant::addVendor($user_id, $d);
                
        // Insert default- vendor location
        vm_ps_restaurant::addVendorLocation($vendor_id, $d);
        
        // Insert default- vendor shipping fee
        vm_ps_restaurant::addVendorShipping($vendor_id, $d);
                
        // Insert default- vendor kitchens hours
        vm_ps_restaurant::addKitchen($vendor_id, $d);
        // Insert default- vendor days closed
        vm_ps_restaurant::addDaysClosed($vendor_id, $d);
        // Insert default- vendor cuisine
        vm_ps_restaurant::addCuisine($vendor_id, $d);
        
        // Category Level 1
        $category_name = $d["vendor_name"];
        $category_parent_id = vm_ps_restaurant::addCategory($vendor_id, $category_name, 0);
        vm_ps_restaurant::addCategoryXref(0, $category_parent_id);
        
        // Category Level 2
        $category_name = $d["vendor_address_1"].', '.$d["vendor_state"].', '.$d["vendor_zip"].', '.$d["vendor_phone_1"];
        $category_id = vm_ps_restaurant::addCategory($vendor_id, $category_name, 0);
        vm_ps_restaurant::addCategoryXref($category_parent_id, $category_id);
        
        $GLOBALS['vmLogger']->info('The Vendor has been added.');
        
        // Insert default- shopper group
        vm_ps_restaurant::addShopperGroup($vendor_id, $d);
        $mess = '<div><br/>Congratulations! You’re restaurant is successfully added.<br/>'.
                'We’re glad to have you as part of the family. If you have any<br/>'.
                'questions about how else to market your restaurant, don’t hesitate to call on us.<br/><br/>'.               
                'Now all you have to do is upload your menu.<br/>'.
                'Remember, we can help you with that and other marketing services.<br/>'.
                'Contact Customer Service for further assistance at Sales@menutise.com.</div>';
        vmRedirect(JURI::root(),$mess);   
        
    }
    
    function addVendor($user_id, $d) {
        $timestamp = time();
        $phone_1 = $d["vendor_phone_1_1"].'-'.$d["vendor_phone_1_2"].'-'.$d["vendor_phone_1_3"];
        $phone_2 = $d["vendor_phone_2_1"].'-'.$d["vendor_phone_2_2"].'-'.$d["vendor_phone_2_3"];
        
        $fields = array('vendor_user_id' => $user_id,
                        'vendor_name' => $d["vendor_name"],
                        'contact_last_name' => 'Restaurant',
                        'contact_first_name' => $d["vendor_name"],
                        'contact_phone_1' => $phone_1,
                        'contact_phone_2' => $phone_2,
                        'contact_email' => $d["vendor_email"],
                        'cdate' => $timestamp,
                        'mdate' => $timestamp,
                        'vendor_image_path' => 'shop_image/',
                        'vendor_currency_display_style' => '1|$|2|.| |2|1',
                        'vendor_accepted_currencies' => 'USD',
                        'vendor_date_format' => '%A, %d %B %Y %H:%M',                        
                        'vendor_keyword' => $d["vendor_keyword"],
                        'vendor_facebook_link' => $d["vendor_facebook_link"],
                        'vendor_descriptions' => $d["vendor_descriptions"]);  
        
        $db = new ps_DB;
        $db->buildQuery('INSERT', '#__{vm}_vendor', $fields );
        $db->query();
        
        $vendor_id = $db->last_insert_id();
        
        return $vendor_id;
    }
    
    function addVendorLocation($vendor_id, $d) {
        $fields = array('vendor_id' => $vendor_id,
                        'vendor_address_1' => $d["vendor_address_1"],
                        'vendor_address_2' => $d["vendor_address_2"],
                        'vendor_city' => $d["vendor_city"],
                        'vendor_state' => $d["vendor_state"],
                        'vendor_zip' => $d["vendor_zip"],
                        'vendor_delivery' => $d["vendor_delivery"],
                        'vendor_currency' => 'USD',
                        'vendor_fax' => $d["vendor_fax"]
                        );
        
        $db = new ps_DB;
        $db->buildQuery('INSERT', '#__{vm}_vendor_locations', $fields );
        $db->query();
    } 
    
    function addVendorShipping($vendor_id, $d) {
        $fields = array('vendor_id' => $vendor_id,
                        'amount' => $d["vendor_delivery_fee"]);
        
        $db = new ps_DB;
        $db->buildQuery('INSERT', '#__{vm}_vendor_shipping', $fields );
        $db->query();
    }
    
    function addShopperGroup($vendor_id, $d) {
        // Insert default- shopper group
        $q = "INSERT INTO #__{vm}_shopper_group (";
        $q .= "`vendor_id`,";
        $q .= "`shopper_group_name`,";
        $q .= "`shopper_group_desc`,`default`) VALUES ('";
        $q .= $vendor_id . "',";
        $q .= "'-default-',";
        $q .= "'Default shopper group for ".$d["vendor_name"]."','1')";
        
        $db = new ps_DB;
        $db->query($q);
    }
    
    function addKitchen($vendor_id, $d) {
        $db = new ps_DB;
        
        $q = 'DELETE FROM #__{vm}_vendor_kitchens_hours WHERE vendor_id = '.$vendor_id;
        $db->query($q);
        
        $ids = $d["hdnKitchen"];
        $arrId = explode(',', $ids);
        for($i=0;$i<count($arrId);$i++) {
            $id = (int)$d["hdnKitchenId".$arrId[$i]];
            
            $type_hour_frm = $d["cboAmPmFrm".$arrId[$i]];
            $hour_from = $d["cboHourFrm".$arrId[$i]];
            $date_from = $d["cboDateFrm".$arrId[$i]];
            
            if ($type_hour_frm == 2) { // PM
                $hour_from = $hour_from + 12;
            }
            if ($hour_from == 24) {
                $hour_from = 0;
                $date_from = $date_from + 1;
                if ($date_from > 7) { // 7 is Sunday, 1 is Monday
                    $date_from = 1;
                }
            }
            
            $type_hour_to = $d["cboAmPmTo".$arrId[$i]];
            $hour_to = $d["cboHourTo".$arrId[$i]];
            $date_to = $d["cboDateTo".$arrId[$i]];
            
            if ($type_hour_to == 2) { // PM
                $hour_to = $hour_to + 12;
            }
            if ($hour_to == 24) {
                $hour_to = 0;
                $date_to = $date_to + 1;
                if ($date_to > 7) { // 7 is Sunday, 1 is Monday
                    $date_to = 1;
                }
            }
            
            if ($id >0) {
                $fields = array('id' => $id,
                            'vendor_id' => $vendor_id,
                            'date_from' => $date_from,
                            'date_to' => $date_to,
                            'hour_from' => $hour_from,
                            'minute_from' => $d["cboMinuteFrm".$arrId[$i]],
                            'hour_to' => $hour_to,
                            'minute_to' => $d["cboMinuteTo".$arrId[$i]]
                            );
            } else {
                $fields = array('vendor_id' => $vendor_id,
                            'date_from' => $date_from,
                            'date_to' => $date_to,
                            'hour_from' => $hour_from,
                            'minute_from' => $d["cboMinuteFrm".$arrId[$i]],
                            'hour_to' => $hour_to,
                            'minute_to' => $d["cboMinuteTo".$arrId[$i]]
                            );
            }
                        
            $db->buildQuery('INSERT', '#__{vm}_vendor_kitchens_hours', $fields );
                        
            $db->query();
        }
    }
    
    /**
    * Convert from MM/dd/yyyy to yyyy-MM-dd
    * 
    * @param mixed $date
    * $date: dd/MM/yyyy
    */
    function convertToDate1($date) {
        $dates = explode('/', $date);
        
        $date = $dates[2].'-'.$dates[0].'-'.$dates[1];
        
        return $date;
    }
    
    /**
    * Convert from yyyy-MM-dd to MM/dd/yyyy
    * 
    * @param mixed $date
    * $date: yyyy-MM-dd
    */
    function convertToDate2($date) {
        $dates = explode('-', $date);
        
        $date = $dates[1].'-'.$dates[2].'-'.$dates[0];
        
        return $date;
    }
    
    function addDaysClosed($vendor_id, $d) {
        $db = new ps_DB;
        
        $q = 'DELETE FROM #__{vm}_vendor_days_closed WHERE vendor_id = '.$vendor_id;
        $db->query($q);
        
        $ids = $d["hdnDayClosed"];
        $arrId = explode(',', $ids);
        for($i=0;$i<count($arrId);$i++) {
            $id = (int)$d["hdnDaysClosedId".$arrId[$i]];
            $date_closed = vm_ps_restaurant::convertToDate1($d["txtDateClosed".$arrId[$i]]);
            
            if ($id > 0) {
                $fields = array('id' => $id,
                                'vendor_id' => $vendor_id,
                                'day_closed' => $date_closed
                                );
            } else {
                $fields = array('vendor_id' => $vendor_id,
                                'day_closed' => $date_closed
                                );
            }
            
            $db->buildQuery('INSERT', '#__{vm}_vendor_days_closed', $fields );
            
            $db->query();
        }
    }

    function addCuisine($vendor_id, $d) {
        $db = new ps_DB;
        
        $q = 'DELETE FROM #__{vm}_vendor_cuisine WHERE vendor_id = '.$vendor_id;
        $db->query($q);
        
        $ids = $d["hdnCuisine"];
        $arrId = explode(',', $ids);
        for($i=0;$i<count($arrId);$i++) {
            $id = (int)$d["hdnCuisineId".$arrId[$i]];
            
            if ($id >0) {
                $fields = array('id' => $id,
                                'vendor_id' => $vendor_id,
                                'cuisine_id' => $d["cboCuisine".$arrId[$i]]
                                );
            } else {
                $fields = array('vendor_id' => $vendor_id,
                                'cuisine_id' => $d["cboCuisine".$arrId[$i]]
                                );
            }
            
            $db->buildQuery('INSERT', '#__{vm}_vendor_cuisine', $fields );
            
            $db->query();
        }
    }
    
    function addUsers($d) {
        $fields = array('username' => $d["vendor_user_name"],
                        'password' => md5($d["vendor_password"]),
                        'email' => $d["vendor_email"],
                        'name' => $d["vendor_name"],
                        'gid' => 19,
                        'usertype' => 'Author',
                        'registerDate' => date('Y-m-d H:i:s'));
        
        $db = new ps_DB;
        $db->buildQuery('INSERT', '#__users', $fields );
        $db->query();
        
        $user_id = $db->last_insert_id();
        
        return $user_id;
    }
    
    function addUsersVmInfo($d, $user_id) {
        $hash_secret = "VirtueMartIsCool";
        $user_info_id = md5(uniqid( $hash_secret));
        $timestamp = time();
        
        $fields = array('user_info_id' => $user_info_id,
                        'user_id' => $user_id,
                        'address_type' => 'BT',
                        'address_type_name' => '-default-',
                        'last_name' => 'Restaurant',
                        'first_name' => $d["vendor_name"],
                        'user_email' => $d["vendor_email"],
                        'address_1' => $d["vendor_address_1"],
                        'address_2' => $d["vendor_address_2"],
                        'city' => $d["vendor_city"],
                        'state' => $d["vendor_state"],
                        'zip' => $d["vendor_zip"],
                        'perms' => 'storeadmin',
                        'cdate' => $timestamp, 
                        'mdate' => $timestamp);
        
        $db = new ps_DB;
        $db->buildQuery('INSERT', '#__{vm}_user_info', $fields );
        $db->query();
    }
    
    function addCoreAclAro($d, $user_id) {
        $fields = array('section_value' => 'users',
                        'value' => $user_id,
                        'name' => $d["vendor_name"],
                        'hidden' => 0);
        
        $db = new ps_DB;
        $db->buildQuery('INSERT', '#__core_acl_aro', $fields );
        $db->query();
        
        $aro_id = $db->last_insert_id();
        
        return $aro_id;
    }
    
    function addCoreAclGroupAroMap($aro_id) {
        $fields = array('group_id' => 19,
                        'aro_id' => $aro_id);
        
        $db = new ps_DB;
        $db->buildQuery('INSERT', '#__core_acl_groups_aro_map', $fields );
        $db->query();
    }
    
    function getListOrder($parent_category_id) {
        $db = new ps_DB;
        
        $q = 'SELECT MAX(list_order) AS list_order 
              FROM #__{vm}_category_xref,#__{vm}_category
              WHERE category_parent_id='.$parent_category_id.'
                AND category_child_id=category_id';
        
        $db->query( $q );
        $db->next_record();

        $list_order = intval($db->f("list_order"))+1;
        
        return $list_order;
    }
    
    function addCategory($vendor_id, $category_name, $category_parent_id) {
        $timestamp = time();
        
        $list_order = ps_restaurant::getListOrder($category_parent_id);
        
        $fields = array('vendor_id' => $vendor_id,
                        'category_name' => addslashes($category_name),
                        'category_publish' => 'Y',
                        'cdate' => $timestamp,
                        'mdate' => $timestamp,
                        'category_browsepage' => 'browse_1',
                        'products_per_row' => 1,
                        'category_flypage' => 'flypage.tpl',
                        'list_order' => $list_order);
        
        $db = new ps_DB;
        $db->buildQuery('INSERT', '#__{vm}_category', $fields );
        $db->query();
        
        $category_id = $db->last_insert_id();
        
        return $category_id;
    }
    
    function addCategoryXref($parent_category_id, $category_id) {
        $fields = array('category_parent_id' => $parent_category_id,
                        'category_child_id' => $category_id);
        
        $db = new ps_DB;
        $db->buildQuery('INSERT', '#__{vm}_category_xref', $fields );
        $db->query();
    }
        
    function update(&$d) {
        global $VM_LANG;
        
        $db = new ps_DB;
        
        foreach ($d as $key => $value) {
            if (!is_array($value))
            $d[$key] = addslashes($value);
        }
        
        // Update to the users table
        vm_ps_restaurant::updateUsers($d);
        // Update to the user_info table
        vm_ps_restaurant::updateUsersVmInfo($d);
        // Update to the core_acl_aro table
        vm_ps_restaurant::updateCoreAclAro($d);
        
        $vendor_id = (int)$d["hdnVendorId"];
        // Update vendor table
        vm_ps_restaurant::updateVendor($vendor_id, $d);
        
        /* Update default- vendor location */
        vm_ps_restaurant::updateVendorLocation($vendor_id, $d);
        
         /* Update default- vendor shipping fee */
        vm_ps_restaurant::updateVendorShippingFee($vendor_id, $d);
        
        // Insert default- vendor kitchens hours
        vm_ps_restaurant::addKitchen($vendor_id, $d);
        // Insert default- vendor days closed
        vm_ps_restaurant::addDaysClosed($vendor_id, $d);
        // Insert default- vendor cuisine
        vm_ps_restaurant::addCuisine($vendor_id, $d);
        
        // Category Level 1
        $category_name = $d["vendor_name"];
        $category_parent_id = $d["hdnCategoryParentId"];
        vm_ps_restaurant::updateCategory($category_parent_id, $category_name);
        
        // Category Level 2
        $category_name = $d["vendor_address_1"].', '.$d["vendor_state"].', '.$d["vendor_zip"].', '.$d["vendor_phone_1"];
        $category_child_id = $d["hdnCategoryChildId"];
        vm_ps_restaurant::updateCategory($category_child_id, $category_name);
        
        if( $d['vendor_id'] == 1 ) {
            $GLOBALS['vmLogger']->info($VM_LANG->_('VM_STORE_UPDATED'));
        } else {
            $GLOBALS['vmLogger']->info($VM_LANG->_('VM_VENDOR_UPDATED'));
        }
        
        return True;
    } 
        
    function updateVendor($vendor_id, $d) {
        $phone_1 = $d["vendor_phone_1_1"].'-'.$d["vendor_phone_1_2"].'-'.$d["vendor_phone_1_3"];
        $phone_2 = $d["vendor_phone_2_1"].'-'.$d["vendor_phone_2_2"].'-'.$d["vendor_phone_2_3"];
        
        $fields = array('vendor_name' => $d["vendor_name"],
                        'contact_last_name' => 'Restaurant',
                        'contact_first_name' => $d["vendor_name"],
                        'contact_phone_1' => $phone_1,
                        'contact_phone_2' => $phone_2,
                        'contact_email' => $d["vendor_email"],
                        'vendor_keyword' => $d["vendor_keyword"],
                        'vendor_facebook_link' => $d["vendor_facebook_link"],
                        'vendor_descriptions' => $d["vendor_descriptions"]);
        
        $db = new ps_DB;       
        $db->buildQuery( 'UPDATE', '#__{vm}_vendor', $fields, 'WHERE vendor_id = '.$vendor_id );
        $db->query();
    }
    
    function updateVendorLocation($vendor_id, $d) {
        $fields = array('vendor_address_1' => $d["vendor_address_1"],
                        'vendor_address_2' => $d["vendor_address_2"],
                        'vendor_city' => $d["vendor_city"],
                        'vendor_state' => $d["vendor_state"],
                        'vendor_zip' => $d["vendor_zip"],
                        'vendor_delivery' => $d["vendor_delivery"],
                        'vendor_fax' => $d["vendor_fax"]
                        );
        
        $db = new ps_DB;
        $db->buildQuery( 'UPDATE', '#__{vm}_vendor_locations', $fields, 'WHERE vendor_id = '.$vendor_id );
        $db->query();
    }
    
    function updateVendorShippingFee($vendor_id, $d) {
        $fields = array('amount' => $d["vendor_delivery_fee"]);
        
        $db = new ps_DB;
        $db->buildQuery( 'UPDATE', '#__{vm}_vendor_shipping', $fields, 'WHERE vendor_id = '.$vendor_id );
        $db->query();
    }
    
    function updateUsers($d) {
        $pass_old = $d["hdnPassword"];
        $pass_curr = $d["vendor_password"];
        if ($pass_old == $pass_curr) {
            $fields = array('username' => $d["vendor_user_name"],
                            'email' => $d["vendor_email"],
                            'name' => $d["vendor_name"]);
        } else {
            $fields = array('username' => $d["vendor_user_name"],
                            'password' => md5($pass_curr),
                            'name' => $d["vendor_name"]);
        }
        
        $db = new ps_DB;
        $db->buildQuery('UPDATE', '#__users', $fields, 'WHERE id = '.$d["hdnUserId"] );
        $db->query();
    }
    
    function updateUsersVmInfo($d) {
        $timestamp = time();
        $fields = array('first_name' => $d["vendor_name"],
                        'user_email' => $d["vendor_email"],
                        'address_1' => $d["vendor_address_1"],
                        'address_2' => $d["vendor_address_2"],
                        'city' => $d["vendor_city"],
                        'state' => $d["vendor_state"],
                        'zip' => $d["vendor_zip"],
                        'mdate' => $timestamp);
        
        $db = new ps_DB;
        $db->buildQuery('UPDATE', '#__{vm}_user_info', $fields, 'WHERE user_id = '.$d["hdnUserId"] );
        $db->query();
    }
    
    function updateCoreAclAro($d) {
        $fields = array('name' => $d["vendor_name"]);
        
        $db = new ps_DB;
        $db->buildQuery('UPDATE', '#__core_acl_aro', $fields, 'WHERE id = '.$d["hdnUserId"] );
        $db->query();
    }
    
    function updateCategory($category_id, $category_name) {
        $timestamp = time();
                
        $fields = array('category_name' => addslashes($category_name),
                        'mdate' => $timestamp);
        
        $db = new ps_DB;
        $db->buildQuery('UPDATE', '#__{vm}_category', $fields, 'WHERE category_id = '.$category_id );
        $db->query();
    }
}

// Check if there is an extended class in the Themes and if it is allowed to use them
// If the class is called outside Virtuemart, we have to make sure to load the settings
// Thomas Kahl - Feb. 2009
if (!defined('VM_ALLOW_EXTENDED_CLASSES') && file_exists(dirname(__FILE__).'/../virtuemart.cfg.php')) {
    include_once(dirname(__FILE__).'/../virtuemart.cfg.php');
}
// If settings are loaded, extended Classes are allowed and the class exisits...
if (defined('VM_ALLOW_EXTENDED_CLASSES') && defined('VM_THEMEPATH') && VM_ALLOW_EXTENDED_CLASSES && file_exists(VM_THEMEPATH.'user_class/'.basename(__FILE__))) {
    // Load the theme-user_class as extended
    include_once(VM_THEMEPATH.'user_class/'.basename(__FILE__));
} else {
    // Otherwise we have to use the original classname to extend the core-class
    class ps_restaurant extends vm_ps_restaurant {}
}  
?>
